/**
* ...
* @author Default
* @version 0.1
*/

package crlclr.ui {
	import caurina.transitions.Tweener;
	import crlclr.data.BookData;
	import crlclr.utils.Padding;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;

	public class ToolThumbTip extends QzUI {
		private var i:int = 0;
		private var data:BookData;
		
		private var startY:int = 80;
		private var endY:int = -5;
		private var padding:Padding;
		
		public var container:Sprite;
		public var currentBitmap:DisplayObject;
		
		
		
		private var bitmapList:Array;
		public function ToolThumbTip() {
			super();
			this.y = startY;
		}
		
		public function set Data(data:BookData):void {
			while(this.container.numChildren){
				this.container.removeChildAt(this.container.numChildren - 1);
			}
			this.data = data;
			this.bitmapList = new Array();
			
			this.i = 0;
			loadNext();
		}
		
		protected override function createChildren():void {
			this.container = new Sprite();
			addChild(this.container);
			
		}
		protected override function Open():void {
			this.padding= new Padding(5, 5);
		}
		
		public function loadNext() {
			if (this.i >= this.data.Length) return;
			var path:String = this.data.getPageData(this.i++).Thumb;
			var index = path.indexOf("?w=");
			path = path.substring(0, index) + "?w=140|70";
			var urlrequest:URLRequest = new URLRequest(path);
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.INIT, initHander);
			loader.load(urlrequest);
		}
		
		private function initHander(event:Event):void {
			var loaderInfo:LoaderInfo = event.target as LoaderInfo;
			loaderInfo.removeEventListener(Event.INIT, initHander);
			
			var content:DisplayObject = loaderInfo.content;
			content.x = -content.width / 2;
			content.y = -content.height;
			bitmapList.push(content);
			loadNext();
		}
		
		
		public function Tip(index:int):void{
			while(this.container.numChildren){
				this.container.removeChildAt(this.container.numChildren - 1);
			}
			currentBitmap = bitmapList[index] as DisplayObject;
			if (currentBitmap == null) {
				return;
			}
			container.addChild(currentBitmap);
			
			var g:Graphics = this.container.graphics;
			g.clear();
			g.beginFill(0xFFFFFF);
			
			var x0:int = currentBitmap.x - padding.padX;
			var y0:int = currentBitmap.y - padding.padY;
			var w:int = currentBitmap.width + 2 * padding.padX;
			var h:int = currentBitmap.height + 2 * padding.padY;
			
			g.drawRect(x0, y0, w, h);				
		}
		
		public function clear() {
		}
		
	}
	
}
